package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"serve/internal/global/model"

	"serve/internal/svc"
	"serve/videoSrv"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoRootUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoRootUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoRootUpdateLogic {
	return &VideoRootUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoRootUpdateLogic) VideoRootUpdate(in *videoSrv.VideoRootUpdateRequest) (*videoSrv.VideoRootUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var videolog = model.VideoUpdateLog{
		UserId:  int(in.Uid),
		VideoId: int(in.Vid),
		Status:  int(in.Status),
		Des:     in.Des,
	}
	var user model.User
	if model.DB.Where("id = ?", in.Uid).Find(&user); user.Root < 2 {
		return nil, fmt.Errorf("用户权限不足")
	}
	update_bg := model.DB.Begin()
	if err := update_bg.Create(&videolog).Error; err != nil {
		update_bg.Rollback()
		return nil, err
	}
	if err := update_bg.Updates(&model.Video{
		Model: gorm.Model{
			ID: uint(in.Vid),
		},
		Status: int(in.Status),
	}).Error; err != nil {
		update_bg.Rollback()
		return nil, err
	}
	update_bg.Commit()
	return &videoSrv.VideoRootUpdateResponse{}, nil
}
